package com.oazon.moco.link.serialport;

import java.util.Arrays;

import android.util.Log;

import com.oazon.moco.link.core.LinkEvent;
import com.oazon.moco.link.core.LinkEventListener;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.event.SystemStatus;
import com.oazon.moco.link.event.SystemStateEvent;
import com.oazon.moco.serialport.SerialSend;

public class SystemStateEventListener extends LinkEventListener {

	public static final String TAG = "IActionhandler";

	public SystemStateEventListener(LinkEventSource... sources) {
		setBlackList(Arrays.asList(sources));
	}

	@Override
	public void onLinkEvent(LinkEvent event) {
		if (filter((LinkEventSource) event.getSource())
				|| !(event instanceof SystemStateEvent)) {
			return;
		}
		SystemStateEvent e = (SystemStateEvent) event;

		if (e.getShutdownToOpenTime() >= 0
				&& e.getWorkStatus().equals(SystemStatus.SHUTDOWN_TO_OPEN_TIME)) {
			SerialSend.getInstance().sendTime(e.getShutdownToOpenTime());
		} else if (e.getWorkStatus() == SystemStatus.SHUTDONW) {
			Log.d(TAG, "Serial===SHUTDONW");
			SerialSend.getInstance().sendStatus(SerialSend.Serial_SHUTDOWN);
		} else if (e.getWorkStatus() == SystemStatus.SHUTDOWN_OPEN_CANCLE) {
			Log.d(TAG, "Serial===SHUTDOWN_OPEN_CANCLE");
			SerialSend.getInstance().sendStatus(
					SerialSend.Serial_SHUTDOWNTOOPEN_TIME_CANCEL);
		} else if (e.getWorkStatus() == SystemStatus.WAIT) {
			Log.d(TAG, "Serial===WAIT");
			SerialSend.getInstance().sendStatus(SerialSend.Serial_SLEEPING);
		} else if (e.getWorkStatus() == SystemStatus.NORMAL) {
			Log.d(TAG, "Serial===NORMAL");
			SerialSend.getInstance().sendStatus(SerialSend.Serial_WAKE);
		} else if (e.getWorkStatus() == SystemStatus.OPENDOWN) {
			Log.d(TAG, "Serial===OPENDOWN");
			SerialSend.getInstance().sendStatus(SerialSend.Serial_OPEN);
		}else if (e.getWorkStatus() == SystemStatus.SCREEN_PROTECT) {
			SerialSend.getInstance().sendStatus(SerialSend.Serial_SCREEN_PROTECT);
		}
	}
}
